package org.vudroid.core;

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.RectF;
import java.lang.ref.SoftReference;
import org.vudroid.core.DecodeService;

/* JADX INFO: Access modifiers changed from: package-private */
/* loaded from: classes2.dex */
public class PageTreeNode {
    private static final int SLICE_SIZE = 65535;
    private Bitmap bitmap;
    private SoftReference<Bitmap> bitmapWeakReference;
    private PageTreeNode[] children;
    private boolean decodingNow;
    private DocumentView documentView;
    private boolean invalidateFlag;
    private final Page page;
    private final RectF pageSliceBounds;
    private Rect targetRect;
    private RectF targetRectF;
    private final int treeNodeDepthLevel;
    private Matrix matrix = new Matrix();
    private final Paint bitmapPaint = new Paint();

    /* JADX INFO: Access modifiers changed from: package-private */
    public PageTreeNode(DocumentView documentView, RectF rectF, Page page, int i, PageTreeNode pageTreeNode) {
        this.documentView = documentView;
        this.pageSliceBounds = evaluatePageSliceBounds(rectF, pageTreeNode);
        this.page = page;
        this.treeNodeDepthLevel = i;
    }

    public void updateVisibility() {
        invalidateChildren();
        PageTreeNode[] pageTreeNodeArr = this.children;
        if (pageTreeNodeArr != null) {
            for (PageTreeNode pageTreeNode : pageTreeNodeArr) {
                pageTreeNode.updateVisibility();
            }
        }
        if (isVisible() && !thresholdHit()) {
            if (getBitmap() != null && !this.invalidateFlag) {
                restoreBitmapReference();
            } else {
                decodePageTreeNode();
            }
        }
        if (isVisibleAndNotHiddenByChildren()) {
            return;
        }
        stopDecodingThisNode();
        setBitmap(null);
    }

    public void invalidate() {
        invalidateChildren();
        invalidateRecursive();
        updateVisibility();
    }

    private void invalidateRecursive() {
        this.invalidateFlag = true;
        PageTreeNode[] pageTreeNodeArr = this.children;
        if (pageTreeNodeArr != null) {
            for (PageTreeNode pageTreeNode : pageTreeNodeArr) {
                pageTreeNode.invalidateRecursive();
            }
        }
        stopDecodingThisNode();
    }

    /* JADX INFO: Access modifiers changed from: package-private */
    public void invalidateNodeBounds() {
        this.targetRect = null;
        this.targetRectF = null;
        PageTreeNode[] pageTreeNodeArr = this.children;
        if (pageTreeNodeArr != null) {
            for (PageTreeNode pageTreeNode : pageTreeNodeArr) {
                pageTreeNode.invalidateNodeBounds();
            }
        }
    }

    /* JADX INFO: Access modifiers changed from: package-private */
    public void draw(Canvas canvas) {
        if (getBitmap() != null) {
            canvas.drawBitmap(getBitmap(), new Rect(0, 0, getBitmap().getWidth(), getBitmap().getHeight()), getTargetRect(), this.bitmapPaint);
        }
        PageTreeNode[] pageTreeNodeArr = this.children;
        if (pageTreeNodeArr == null) {
            return;
        }
        for (PageTreeNode pageTreeNode : pageTreeNodeArr) {
            pageTreeNode.draw(canvas);
        }
    }

    private boolean isVisible() {
        return RectF.intersects(this.documentView.getViewRect(), getTargetRectF());
    }

    private RectF getTargetRectF() {
        if (this.targetRectF == null) {
            this.targetRectF = new RectF(getTargetRect());
        }
        return this.targetRectF;
    }

    /* JADX INFO: Access modifiers changed from: private */
    public void invalidateChildren() {
        if (thresholdHit() && this.children == null && isVisible()) {
            int i = this.treeNodeDepthLevel * 2;
            this.children = new PageTreeNode[]{new PageTreeNode(this.documentView, new RectF(0.0f, 0.0f, 0.5f, 0.5f), this.page, i, this), new PageTreeNode(this.documentView, new RectF(0.5f, 0.0f, 1.0f, 0.5f), this.page, i, this), new PageTreeNode(this.documentView, new RectF(0.0f, 0.5f, 0.5f, 1.0f), this.page, i, this), new PageTreeNode(this.documentView, new RectF(0.5f, 0.5f, 1.0f, 1.0f), this.page, i, this)};
        }
        if ((thresholdHit() || getBitmap() == null) && isVisible()) {
            return;
        }
        recycleChildren();
    }

    private boolean thresholdHit() {
        float zoom = this.documentView.zoomModel.getZoom();
        int width = this.documentView.getWidth();
        float pageHeight = this.page.getPageHeight(width, zoom);
        int i = this.treeNodeDepthLevel;
        return ((((float) width) * zoom) * pageHeight) / ((float) (i * i)) > 65535.0f;
    }

    public Bitmap getBitmap() {
        SoftReference<Bitmap> softReference = this.bitmapWeakReference;
        if (softReference != null) {
            return softReference.get();
        }
        return null;
    }

    private void restoreBitmapReference() {
        setBitmap(getBitmap());
    }

    private void decodePageTreeNode() {
        if (isDecodingNow()) {
            return;
        }
        setDecodingNow(true);
        this.documentView.decodeService.decodePage(this, this.page.index, new DecodeService.DecodeCallback() { // from class: org.vudroid.core.PageTreeNode.1
            @Override // org.vudroid.core.DecodeService.DecodeCallback
            public void decodeComplete(final Bitmap bitmap) {
                PageTreeNode.this.documentView.post(new Runnable() { // from class: org.vudroid.core.PageTreeNode.1.1
                    @Override // java.lang.Runnable
                    public void run() {
                        PageTreeNode.this.setBitmap(bitmap);
                        PageTreeNode.this.invalidateFlag = false;
                        PageTreeNode.this.setDecodingNow(false);
                        PageTreeNode.this.page.setAspectRatio(PageTreeNode.this.documentView.decodeService.getPageWidth(PageTreeNode.this.page.index), PageTreeNode.this.documentView.decodeService.getPageHeight(PageTreeNode.this.page.index));
                        PageTreeNode.this.invalidateChildren();
                    }
                });
            }
        }, this.documentView.zoomModel.getZoom(), this.pageSliceBounds);
    }

    private RectF evaluatePageSliceBounds(RectF rectF, PageTreeNode pageTreeNode) {
        if (pageTreeNode == null) {
            return rectF;
        }
        Matrix matrix = new Matrix();
        matrix.postScale(pageTreeNode.pageSliceBounds.width(), pageTreeNode.pageSliceBounds.height());
        matrix.postTranslate(pageTreeNode.pageSliceBounds.left, pageTreeNode.pageSliceBounds.top);
        RectF rectF2 = new RectF();
        matrix.mapRect(rectF2, rectF);
        return rectF2;
    }

    /* JADX INFO: Access modifiers changed from: private */
    public void setBitmap(Bitmap bitmap) {
        Bitmap bitmap2;
        if ((bitmap != null && bitmap.getWidth() == -1 && bitmap.getHeight() == -1) || (bitmap2 = this.bitmap) == bitmap) {
            return;
        }
        if (bitmap != null) {
            if (bitmap2 != null) {
                bitmap2.recycle();
            }
            this.bitmapWeakReference = new SoftReference<>(bitmap);
            this.documentView.postInvalidate();
        }
        this.bitmap = bitmap;
    }

    private boolean isDecodingNow() {
        return this.decodingNow;
    }

    /* JADX INFO: Access modifiers changed from: private */
    public void setDecodingNow(boolean z) {
        if (this.decodingNow != z) {
            this.decodingNow = z;
            if (z) {
                this.documentView.progressModel.increase();
            } else {
                this.documentView.progressModel.decrease();
            }
        }
    }

    private Rect getTargetRect() {
        if (this.targetRect == null) {
            this.matrix.reset();
            this.matrix.postScale(this.page.bounds.width(), this.page.bounds.height());
            this.matrix.postTranslate(this.page.bounds.left, this.page.bounds.top);
            RectF rectF = new RectF();
            this.matrix.mapRect(rectF, this.pageSliceBounds);
            this.targetRect = new Rect((int) rectF.left, (int) rectF.top, (int) rectF.right, (int) rectF.bottom);
        }
        return this.targetRect;
    }

    private void stopDecodingThisNode() {
        if (isDecodingNow()) {
            this.documentView.decodeService.stopDecoding(this);
            setDecodingNow(false);
        }
    }

    private boolean isHiddenByChildren() {
        PageTreeNode[] pageTreeNodeArr = this.children;
        if (pageTreeNodeArr == null) {
            return false;
        }
        for (PageTreeNode pageTreeNode : pageTreeNodeArr) {
            if (pageTreeNode.getBitmap() == null) {
                return false;
            }
        }
        return true;
    }

    private void recycleChildren() {
        PageTreeNode[] pageTreeNodeArr = this.children;
        if (pageTreeNodeArr == null) {
            return;
        }
        for (PageTreeNode pageTreeNode : pageTreeNodeArr) {
            pageTreeNode.recycle();
        }
        if (childrenContainBitmaps()) {
            return;
        }
        this.children = null;
    }

    private boolean containsBitmaps() {
        return getBitmap() != null || childrenContainBitmaps();
    }

    private boolean childrenContainBitmaps() {
        PageTreeNode[] pageTreeNodeArr = this.children;
        if (pageTreeNodeArr == null) {
            return false;
        }
        for (PageTreeNode pageTreeNode : pageTreeNodeArr) {
            if (pageTreeNode.containsBitmaps()) {
                return true;
            }
        }
        return false;
    }

    private void recycle() {
        stopDecodingThisNode();
        setBitmap(null);
        PageTreeNode[] pageTreeNodeArr = this.children;
        if (pageTreeNodeArr != null) {
            for (PageTreeNode pageTreeNode : pageTreeNodeArr) {
                pageTreeNode.recycle();
            }
        }
    }

    private boolean isVisibleAndNotHiddenByChildren() {
        return isVisible() && !isHiddenByChildren();
    }
}
